package com.freewater.springboot.demo.aop.advice.generic;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

import java.util.Collection;

@Aspect
public class IServiceAspect {

    @Pointcut("execution(* com.freewater.springboot.demo.aop.advice.generic.IService+.describe(*))")
    public void describeMethod() {

    }

    @Pointcut("execution(* com.freewater.springboot.demo.aop.advice.generic.IService+.describeCollection(*))")
    public void describeCollectionMethod() {

    }

    // 拦截IService实现类，且传入describe方法的参数类型为Person，因此只拦截PersonService的describe方法，而UserService的describe方法不会被拦截
    @Before(value = "describeMethod() && args(person)", argNames = "joinPoint,person")
    public void beforeAdvice(JoinPoint joinPoint, Person person) {
        System.out.println("params person: " + person);
    }

    // 注意：describeCollection方法的参数为Collection类型，Spring AOP无法根据collection中的类型参数实际值过滤。例如这样写：
    // public void beforeAdvice2(JoinPoint joinPoint, Collection<Person> param，本意想只拦截PersonService（IService的实现类），但是
    // 实际也会拦截UserService（IService的另外一个实现类）的describeCollection方法调用；
    @Before(value = "describeCollectionMethod() && args(param)")
    public void beforeAdvice2(JoinPoint joinPoint, Collection<?> param) {
        System.out.println("params persons: " + param);
    }
}
